/*** In The Name of Allah ***/
package com.codeanalysissys.backend.graphs.cfg;

import com.codeanalysissys.backend.cpp.CppCFGBuilder;
import com.codeanalysissys.backend.golang.GoCFGBuilder;
import com.codeanalysissys.backend.java.JavaCFGBuilder;
import com.codeanalysissys.backend.javascript.JavaScriptCFGBuilder;
import com.codeanalysissys.backend.python.PythonCFGBuilder;
import com.codeanalysissys.backend.ruby.RubyCFGBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Control Flow Graph (CFG) Builder.
 * This class invokes the appropriate builder based on the given language parameter.
 *
 * @author Seyed Mohammad Ghaffarian
 */
public class CFGBuilder {

    /**
     * Build and return the CFG of the given source code file with specified language.
     */
    public static ControlFlowGraph build(String lang, String srcFilePath) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppCFGBuilder.build(srcFilePath);
            case "C":
                return null;
            case "Java":
                return JavaCFGBuilder.build(srcFilePath);
            case "JavaScript":
                return JavaScriptCFGBuilder.build(srcFilePath);
            case "Ruby":
                return RubyCFGBuilder.build(srcFilePath);
            case "Python":
                return PythonCFGBuilder.build(srcFilePath);
            case "Golang":
                return GoCFGBuilder.build(srcFilePath);
            default:
                return null;
        }
    }

    public static ControlFlowGraph build(String lang, String fileName, InputStream inputStream) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppCFGBuilder.build(fileName, inputStream);
            case "C":
                return null;
            case "Java":
                return JavaCFGBuilder.build(fileName, inputStream);
            case "JavaScript":
                return JavaScriptCFGBuilder.build(fileName, inputStream);
            case "Python":
                return PythonCFGBuilder.build(fileName, inputStream);
            case "Golang":
                return GoCFGBuilder.build(fileName, inputStream);
            case "Ruby":
                return RubyCFGBuilder.build(fileName, inputStream);
            default:
                return null;
        }
    }


    /**
     * Build and return the CFG of the given content with specified language.
     */
    public static ControlFlowGraph build(String lang, InputStream content) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppCFGBuilder.build(content);
            case "C":
                return null;
            case "Java":
                return JavaCFGBuilder.build(content);
            case "JavaScript":
                return JavaScriptCFGBuilder.build(content);
            case "Python":
                return PythonCFGBuilder.build(content);
            case "Golang":
                return GoCFGBuilder.build(content);
            case "Ruby":
                return RubyCFGBuilder.build(content);
            default:
                return null;
        }
    }
}
